package com.cmcc.linlaker.ui.query.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.itheima.library.PhotoView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class CustomPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<String> mData;

    /**
     * 构造函数
     * 初始化上下文和数据
     */
    public CustomPagerAdapter(Context context, List<String> list) {
        mContext = context;
        mData = list;
    }

    /**
     * 返回要滑动的VIew的个数
     *
     * @return
     */
    @Override
    public int getCount() {
        return mData.size();
    }

    /**
     * 1.将当前视图添加到container中
     * 2.返回当前View
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = View.inflate(mContext, R.layout.view_image_item, null);
        PhotoView photoView = view.findViewById(R.id.image);
        photoView.enable();

        String image_url = mData.get(position);
        ImageLoader.getInstance()
                .displayImage(image_url, photoView, LinhuApp.options);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity) mContext).finish();
            }
        });

        container.addView(view);
        return view;
    }

    /**
     * 从当前container中删除指定位置（position）的View
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container,position,object); 这一句要删除，否则报错
        container.removeView((View) object);
    }

    /**
     * 确定页视图是否与特定键对象关联
     *
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}